package com.buddy.learn.network.nio.channel;


import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;

/**
 * @author WongZP
 * @date 2022/4/19
 */
public class NIOClient {

    public static void main(String[] args) {
        // 打开客户端通道
        try {
            SocketChannel socketChannel = SocketChannel.open();
            socketChannel.connect(new InetSocketAddress("127.0.0.1",8088));
            socketChannel.write(ByteBuffer.wrap("hello nio!".getBytes(StandardCharsets.UTF_8)));
            ByteBuffer allocate = ByteBuffer.allocate(1024);
            int read = socketChannel.read(allocate);
            System.out.println(new String(allocate.array(),0,read));
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
        //
    }
}
